using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace Starznet.SharePoint.SPDAT.ApplicationPages.Settings
{
    public class ManageWebPropertiesPage : StarznetBaseLayoutPage
    {
        protected SPGridView GvWebProperties;
        protected HyperLink HlAddWebProperty;
        protected Literal LitDateCreated;
        protected Literal LitTemplate;

        private PropertyTypes _oPropType = PropertyTypes.SiteCollection;

        public enum PropertyTypes
        {
            SiteCollection,
            Web
        }

        private string QS
        {
            get
            {
                string sQs = string.Empty;
                if (Request.QueryString.Get("isroot") != null)
                    sQs += "isroot=" + Request.QueryString["isroot"].ToString();
                
                return sQs;
            }
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            //Determine the property type
            if(Request.QueryString.Get("isroot") != null)
            {
                if(Request.QueryString["isroot"].ToString() == "false")
                    _oPropType = PropertyTypes.Web;
                else
                    _oPropType = PropertyTypes.SiteCollection;
            }

            switch(_oPropType)
            {
                case PropertyTypes.SiteCollection:
                    this.GvWebProperties.DataSource = GetProperties(true);
                    break;
                case PropertyTypes.Web:
                    this.GvWebProperties.DataSource = GetProperties(false);
                    break;
            }

            this.GvWebProperties.DataBind();
            HlAddWebProperty.NavigateUrl = "AddEditWebProperty.aspx?" + QS;
        }

        public void GvWebProperties_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                HyperLink oLnk = e.Row.FindControl("LnkAddEdit") as HyperLink;
                string sPropName = ((DataRowView)e.Row.DataItem)["PropertyName"].ToString();
                oLnk.Text = sPropName;
                oLnk.NavigateUrl = "AddEditWebProperty.aspx?Property=" + sPropName + "&" + QS;
            }
        }

        private DataTable GetProperties(bool IsRootWeb)
        {
            DataTable oTable = new DataTable();
            oTable.Columns.Add(new DataColumn("PropertyName"));
            oTable.Columns.Add(new DataColumn("PropertyValue"));

            SPWeb oWeb = (IsRootWeb) ? SPContext.Current.Site.RootWeb : SPContext.Current.Web;

            LitTemplate.Text = oWeb.WebTemplate;
            LitDateCreated.Text = oWeb.Created.ToString();

            DataRow oRow = null;
            foreach (DictionaryEntry oKey in oWeb.Properties)
            {
                oRow = oTable.NewRow();
                oRow["PropertyName"] = oKey.Key;
                oRow["PropertyValue"] = oKey.Value;

                oTable.Rows.Add(oRow);
            }
            return oTable;
        }
    }
}
